package org.robert.export_client.excel.metadata;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.robert.export_client.excel.entity.CellStyleEntity;
import org.robert.export_client.excel.util.ExcelUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * ExcelWorkbookBuilder:
 *
 * @author zhaoshengtao
 * @date 2024/01/19
 */
@Data
public abstract class AbstractWorkbookBuilder {

    protected OutputStream outputStream;

    protected HttpServletResponse response;

    protected String path;

    protected HSSFWorkbook workbook;

    protected String fileName = String.valueOf(System.currentTimeMillis());

    protected AbstractSheetBuilder sheetBuilder;

    protected boolean flag = true;

    public AbstractWorkbookBuilder(HSSFWorkbook workbook, String path, String fileName) {
        this.workbook = workbook;
        if(StringUtils.isNotBlank(fileName)){
            this.fileName = fileName;
        }
        this.path = path;
    }

    public AbstractWorkbookBuilder(OutputStream outputStream, HttpServletResponse response,
                                   HSSFWorkbook workbook , String fileName) throws UnsupportedEncodingException {
        this.outputStream = outputStream;
        this.response = response;
        this.workbook = workbook;
        this.fileName = fileName;
        ExcelUtil.setResponse(response, fileName);
    }

    public abstract AbstractSheetBuilder createSheet(String sheetName, Class<?> clazz,
                                            Map<String, Map<String, String>> dictMap) throws Exception;

    public abstract AbstractSheetBuilder createSheet(String sheetName, Class<?> clazz,
                                                   Map<String, Map<String, String>> dictMap,  CellStyleEntity titleStyle) throws Exception ;

    public abstract SourceSheetBuilder createSheet(String sheetName, Class<?> clazz,
                                            Map<String, Map<String, String>> dictMap, Integer limit) throws Exception ;


    public abstract SourceSheetBuilder createSheet(String sheetName, Class<?> clazz,
                                                   Map<String, Map<String, String>> dictMap, Integer limit,  CellStyleEntity titleStyle) throws Exception ;

    public void doWrite(List<?> list) throws Exception {
        this.sheetBuilder.doWrite(list);
    }

    public AbstractWorkbookWrite build(){
        return new AbstractWorkbookWrite(this);
    }

}
